<?php

namespace addons\shopro\model;
use think\Model;
use addons\shopro\model\Goods;
/**
 * 千人千面模型
 */
class PersonalExperience extends Model
{
    // 表名,不含前缀
    protected $name = 'shopro_personal_experience';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * params 请求参数
     * is_page 是否分页
     */
    public static function getGoodsList($params, $is_page = true){
        extract(self::analysisCondition($params));
        if(empty($personal_experience_ids)){
            $goods = Goods::where($condition);
        }else{
            $goods = Goods::where($condition)->where(function ($query) use ($personal_experience_ids) {
                // 所有标签使用 find_in_set or 匹配，亲测速度并不慢
                foreach($personal_experience_ids as $key => $personal_experience_id) {
                    $query->whereOrRaw("find_in_set($personal_experience_id, personal_experience_ids)");
                }
            });
        }
        $goods = $goods->field('*,(sales + show_sales) as total_sales')->orderRaw('weigh desc');
        if ($is_page) {
            $goods = $goods->paginate($per_page ?? 10);
            $goodsData = $goods->items();
        } else {
            $goods = $goodsData = $goods->select();
        }
        $data = [];
        if ($goodsData) {
            $collection = collection($goodsData);
            $data = $collection->hidden(Goods::$list_hidden);
            // 处理活动
            $data->load('skuPrice');        // 延迟预加载
            foreach ($data as $key => $g) {
                $data[$key] = Goods::operActivity($g, $g['sku_price']);
            }
        }

        if ($is_page) {
            $goods->data = $data;
        } else {
            $goods = $data;
        }
        return $goods;
    }

    public static function analysisCondition($params){
        extract($params);
        $condition = [
            'status' => ['in', ((isset($type) && $type == 'all') ? ['up', 'hidden'] : ['up'])]
        ];
        //获取最近10条浏览记录组合标签ID数组
        $user = User::info();
        $viewGoodsIds = UserView::where(['user_id' => $user->id])->order(array('updatetime'=>'DESC'))->limit(10)->column('goods_id');
        //获取最近20条订单记录组合标签ID数组
        $orders = Order::where(['user_id' => $user->id])->with(array('item'=>function($query){$query->field('order_id,goods_id,refund_status,aftersale_status,dispatch_status,dispatch_type');}))->limit(20)->select();
        $orderGoodsIds = array();
        foreach($orders as $order){
            foreach($order['item'] as $item){
                $orderGoodsIds[] = $item['goods_id'];
            }
        }
        //得到浏览和购买过的所有商品的ID
        $goodsIds = array_values(array_unique(array_merge($viewGoodsIds,$orderGoodsIds)));
        //获取所有所有商品ID对应的千人千面标签
        $personal_experience_ids = Goods::where(array('id'=>array('in',$goodsIds),'personal_experience_ids'=>array('<>','')))->column('personal_experience_ids');
        if(!empty($personal_experience_ids)){
            //所有标签组成一个以英文“,”连接的字符串
            $personal_experience_ids = implode(',',$personal_experience_ids);
            //以英文“,”分离所有标签组成的字符串
            $personal_experience_ids = explode(',',$personal_experience_ids);
            //标签数组去重得到标签数组
            $personal_experience_ids = array_values(array_unique($personal_experience_ids));
        }
        return compact('condition','personal_experience_ids');
    }
}
?>